<template>
  <div class="mystar-page">
    <van-nav-bar
      title="我的收藏"
      left-arrow
      @click-left="onClickLeft"
      fixed
      placeholder
      :border="false"
      :safe-area-inset-top="true"
    />
    
    <div class="star-list" v-if="starList.length > 0">
      <div class="food-item" v-for="item in starList" :key="item.name">
        <img :src="item.img" class="food-icon" />
        <div class="food-info">
          <div class="food-name">{{ item.name }}</div>
          <div class="food-meta">
            <span>{{ item.kcal }}大卡</span>
            <span class="dot"></span>
            <span>{{ item.gram }}克</span>
          </div>
        </div>
        <van-icon 
          name="star" 
          class="star-icon" 
          color="#ffd700"
          @click="removeStar(item)"
        />
      </div>
    </div>
    
    <div class="empty-state" v-else>
      <van-empty description="暂无收藏" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { db, STORES } from '../utils/db'

const router = useRouter()
const starList = ref([])

const onClickLeft = () => {
  router.back()
}

const removeStar = async (item) => {
  const userEmail = localStorage.getItem('userEmail')
  if (!userEmail) {
    showToast('请先登录')
    router.push('/login')
    return
  }

  try {
    const user = await db.get(STORES.USER_LIST, userEmail)
    if (!user) {
      showToast('用户信息不存在')
      return
    }
    
    if (!user.starList) {
      user.starList = []
    }
    
    const starIndex = user.starList.findIndex(
      starredItem => starredItem.name === item.name
    )
    
    if (starIndex !== -1) {
      user.starList.splice(starIndex, 1)
      await db.put(STORES.USER_LIST, user)
      starList.value = user.starList
      showToast('取消收藏')
    }
  } catch (error) {
    console.error('取消收藏失败:', error)
    showToast('操作失败，请重试')
  }
}

onMounted(async () => {
  const userEmail = localStorage.getItem('userEmail')
  if (userEmail) {
    try {
      const user = await db.get(STORES.USER_LIST, userEmail)
      if (user && user.starList) {
        starList.value = user.starList
      }
    } catch (error) {
      console.error('获取收藏列表失败:', error)
      showToast('获取收藏列表失败')
    }
  } else {
    showToast('请先登录')
    router.push('/login')
  }
})
</script>

<style scoped>
.mystar-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #e9eafc 0%, #e6e6fa 100%);
}

.van-nav-bar {
  background: #fff !important;
}

.van-nav-bar :deep(.van-nav-bar__title) {
  color: #333;
}

.van-nav-bar :deep(.van-icon) {
  color: #333;
}

.star-list {
  padding: 16px;
  padding-top: 8px;
}

.food-item {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px #e0e0e0;
}

.food-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  object-fit: cover;
  margin-right: 16px;
}

.food-info {
  flex: 1;
}

.food-name {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 4px;
}

.food-meta {
  font-size: 13px;
  color: #888;
  display: flex;
  align-items: center;
}

.dot {
  display: inline-block;
  width: 4px;
  height: 4px;
  background: #888;
  border-radius: 50%;
  margin: 0 8px;
}

.star-icon {
  font-size: 24px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.star-icon:hover {
  transform: scale(1.1);
}

.empty-state {
  padding-top: 100px;
}
</style>